var dataMap = {};
for(a in pojos) {
    var pojo = pojos[a];
    dataMap[a] = {};
}

var modulesGenerator = {
createOne:function(key){
    var pojo = pojos[key];
    var d = {};
    for(i in pojo.columns) {
        d[pojo.columns[i]] = "";
    }
    pojo.setId(d);
    return d;
}
};

var htmlControler = {
loadDom:function(key) {
    var pojo = modulesGenerator.createOne(key);
    var fieldset = $("<fieldset></fieldset>");
    fieldset.attr("id",key+"DataGenerateField");
    var legend = $("<legend></legend>");
    legend.text(pojos[key].name+"生成工具");
    fieldset.append(legend);
    for(i in pojo) {
        var label = $("<label></label>");
        label.text(i);
        var input = $("<input />");
        fieldset.append(label).append(input);
    }
    var generateButton = $("<button onclick=\"bizMethod.generatePojoToDom('"+key+"')\">生成</button>")
    var submitButton = $("<button onclick=\"bizMethod.submit('"+key+"')\">提交</button>")
    fieldset.append(generateButton).append(submitButton);
    $("body").append(fieldset);
    
    var table = $("<table id=\""+key+"DataTable\" border=1><caption>"+pojos[key].name+"列表</caption></table>");
    var tr = $("<tr></tr>");
    for(i in pojo) {
        var th = $("<th>"+i+"</th>");
        tr.append(th);
    }
    table.append(tr);
    $("body").append(table);
},
loadGeneratePojoToDom:function(d,key) {
    var i = 0;
    for(a in d) {
        $($("#"+key+"DataGenerateField input")[i]).val(d[a]);
        i++;
    }
},
loadGeneratePojoFromDom:function(key){
    var d = modulesGenerator.createOne(key);
    var i = 0;
    for(a in d) {
        d[a] = $($("#"+key+"DataGenerateField input")[i]).val();
        i++;
    }
    $("#"+key+"DataGenerateField input").val("");
    return d;
},
flushDataTable:function(key) {
    $("#"+key+"DataTable").find("tr").each(function(){
                                           if(this.firstChild.nodeName == "TD") {
                                           this.parentNode.removeChild(this);
                                           }
                                           });
    for(d in dataMap[key]) {
        var data = dataMap[key][d];
        var tds = [];
        for(a in data) {
            tds.push(data[a]);
        }
        var tr = this.newTR(tds);
        var id=Math.uuid();
        tr.attr("id",id);
        tr.attr("bizId",pojos[key].getId(data));
        $("#"+key+"DataTable").append(tr);
        //this.bindErpTrContextMenu("#"+id);
    }
},
    newTR : function(tds) {
        var tr = $("<tr></tr>");
        for(var i=0; i<tds.length; i++) {
            var td =$("<td></td>");
            td.text(tds[i]);
            tr.append(td);
        }
        return tr;
    }
};


var bizMethod = {
generatePojoToDom:function(key){
    var d = modulesGenerator.createOne(key);
    htmlControler.loadGeneratePojoToDom(d,key);
},
submit:function(key) {
    var d = htmlControler.loadGeneratePojoFromDom(key);
    dataMap[key][pojos[key].getId(d)]=d;
    htmlControler.flushDataTable(key);
}
};

$(function(){
  for(a in pojos) {
  htmlControler.loadDom(a);
  }
  });